<template>
  <div>
    <el-card class="!border-none" shadow="never">
      <el-form
        ref="formRef"
        class="mb-[-16px] mt-[15px]"
        :model="queryParams"
        :inline="true"
        @submit.native.prevent
      >
        <el-form-item label="服务名称">
          <el-input
            class="w-[240px] h-[32px]"
            v-model="queryParams.name"
            placeholder="请输入服务名称"
            clearable
            @keyup.enter="resetPage"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="!border-none mt-4" shadow="never">
      <el-button type="primary" @click="handleAdd"> 新增保障服务 </el-button>

      <el-table size="large" class="mt-[14px]" :data="pager.lists">
        <el-table-column label="服务条款" prop="name" min-width="140" />
        <el-table-column label="服务条款图标" prop="image" min-width="120">
          <template #default="{ row }">
            <div class="flex leading-[40px]">
              <el-avatar :src="row.image" :size="40" />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="服务内容描述" prop="content" min-width="300" />
        <el-table-column label="使用商品数" prop="goods_num" min-width="130" />
        <el-table-column label="状态" prop="status" min-width="90">
          <template #default="{ row }">
            <el-switch
              v-model="row.status"
              :active-value="1"
              :inactive-value="0"
              @click="handleChange(row)"
            />
          </template>
        </el-table-column>
        <el-table-column label="排序" prop="sort" min-width="120" />
        <el-table-column label="创建时间" prop="create_time" min-width="160" />
        <el-table-column label="操作" min-width="150" fixed="right">
          <template #default="{ row }">
            <el-button
              v-perms="['auth.admin/edit']"
              type="primary"
              link
              @click="handleEdit(row)"
            >
              编辑
            </el-button>
            <el-button
              v-if="row.root != 1"
              type="danger"
              link
              @click="handleDelete(row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="flex justify-end mt-4">
        <pagination v-model="pager" />
      </div>
    </el-card>
    <edit-popup
      v-if="showEdit"
      ref="editRef"
      @success="getLists"
      @close="showEdit = false"
    />
  </div>
</template>

<script lang="ts" setup>
import { usePaging } from "@/hooks/usePaging";
import {
  getguaranteeLists,
  delguarantee,
  changeguaranteeStatus,
} from "@/api/goods";
import EditPopup from "./edit.vue";
import feedback from "@/utils/feedback";

const queryParams = reactive({
  name: "",
});
const { pager, getLists, resetPage, resetParams } = usePaging({
  fetchFun: getguaranteeLists,
  params: queryParams,
});
//初始化数据
getLists();
// 展开edit
const editRef = shallowRef<InstanceType<typeof EditPopup>>();
const showEdit = ref(false);
const handleAdd = async () => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("add");
};
const handleEdit = async (data: any) => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("edit");
  editRef.value?.setFormData(data);
};
// 删除数据
const handleDelete = async (id: number) => {
  await feedback.confirm("确定要删除？");
  await delguarantee({ id });
  getLists();
};
//改变状态
const handleChange = async (params: any) => {
  await changeguaranteeStatus(params);
};
</script>
<style lang="scss" scope>
//TODO
.el-alert--warning.is-light {
  background-color: rgba(246, 248, 255, 1);
  color: rgba(64, 115, 250, 1);
}
</style>
